package my.system;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

import my.bean.Admin;
import my.bean.Cart;
import my.bean.Credits;
import my.bean.Customer;
import my.bean.Goods;
import my.service.IAdminService;
import my.service.ICartService;
import my.service.ICreditsService;
import my.service.ICustomerService;
import my.service.IGoodsService;
import my.service.Impl.IAdminServiceImpl;
import my.service.Impl.ICartServiceImpl;
import my.service.Impl.ICreditsServiceImpl;
import my.service.Impl.ICustomerServiceImpl;
import my.service.Impl.IGoodsServiceImpl;

public class CmsSystem {
	private Customer customer;
	private Admin admin;
	private Scanner in;
	private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

	public void enter() {
		while (true) {
			intiface();
			int choose = Loginchoise();// 当选择不合符合要求则退出系统
			if (choose == 1) {
				int innerChoose = 0;
				while (innerChoose != 9) {
					userEnterFace();
					innerChoose = userEnter();
				}
			} else {
				adminLogin();
				while (admin != null) {
					adminEnterFace();
					adminEnter();
				}
			}
		}
	}

	private void intiface() {
		System.out.println("--------------------------------------------------------");
		System.out.println("----------------    欢迎来到千寻网      --------------------");
		System.out.println("--------------------------------------------------------");
		System.out.println("----请选择登录对象:                               -------");
		System.out.println("----------------------1) 客户                                         -------");
		System.out.println("------------------------------2) 管理员                     -------");
		System.out.println("[ 若输入其他则退出系统哦~ ]     --------------------------");
		System.out.println("---------------------------------------------------------");
		System.out.print("请选择:");
	}

	private int Loginchoise() {
		in = new Scanner(System.in);
		int choose = 0;
		try {
			choose = in.nextInt();
			if (choose != 1 && choose != 2) {
				System.out.println("已退出系统~欢迎下次使用~");
				System.exit(0);
			}
		} catch (InputMismatchException e) {
			System.out.println("已退出系统~欢迎下次使用~");
			System.exit(0);
		}
		return choose;
	}

	private void userLogin() {
		in = new Scanner(System.in);
		ICustomerService customerService = new ICustomerServiceImpl();
		String account;
		String password;
		for (int i = 0; i < 3; i++) {
			System.out.println("--------------------------------------------------------");
			System.out.println("-----                   请登录                                          -----");
			System.out.println("--------------------------------------------------------");
			System.out.print("请输入你的账号:");
			account = in.nextLine();
			System.out.print("请输入你的密码:");
			password = in.nextLine();
			System.out.println("--------------------------------------------------------");

			customer = customerService.findByAccountAndPassword(account, password);

			if (customer != null) {
				System.out.println("登录成功!欢迎" + customer.getAccount() + "!");
				System.out.println("--------------------------------------------------------");
				break;
			} else if (i < 2) {
				System.out.println("登录失败~");
				System.out.println("你还有" + (2 - i) + "次机会哦~");
				System.out.println("--------------------------------------------------------");
			} else {
				System.out.println("登录失败次数超过3次了~");
				System.out.println("返回上一级~");
				System.out.println("--------------------------------------------------------");
			}
		}
	}

	private void adminLogin() {
		in = new Scanner(System.in);
		IAdminService adminService = new IAdminServiceImpl();
		String account;
		String password;
		for (int i = 0; i < 3; i++) {
			System.out.println("--------------------------------------------------------");
			System.out.println("-----                   请登录                                          -----");
			System.out.println("--------------------------------------------------------");
			System.out.print("请输入你的管理员账号:");
			account = in.nextLine();
			System.out.print("请输入你的管理员密码:");
			password = in.nextLine();
			System.out.println("--------------------------------------------------------");

			admin = adminService.findByAccountAndPassword(account, password);

			if (admin != null) {
				System.out.println("登录成功!欢迎 " + admin.getAccount() + " 管理员");
				System.out.println("--------------------------------------------------------");
				break;
			} else if (i < 2) {
				System.out.println("登录失败~");
				System.out.println("你还有" + (2 - i) + "次机会哦~");
				System.out.println("--------------------------------------------------------");
			} else {
				System.out.println("登录失败次数超过3次了~");
				System.out.println("返回上一级~");
				System.out.println("--------------------------------------------------------");
			}
		}
	}

	private int userEnter() {
		in = new Scanner(System.in);
		int choose = 0;
		System.out.print("请输入你的选择:");
		try {
			in.reset();
			choose = in.nextInt();
		} catch (InputMismatchException e) {
			choose = 7;
			System.out.println("你都输入了啥子啊?返回上一级去吧!");
		}

		switch (choose) {
		case 1:
			if (customer != null) {
				System.out.println("--------------------------------------------------------");
				System.out.println("------你都已经登录过啦,别搞~ 选其他选项去!----------------");
				System.out.println("--------------------------------------------------------");
			} else {
				regFace();
				regOpeartor();
			}
			break;
		case 2:
			if (customer != null) {
				System.out.println("--------------------------------------------------------");
				System.out.println("------你都已经登录过啦,别搞~ 选其他选项去!----------------");
				System.out.println("--------------------------------------------------------");
			} else {
				userLogin();
			}
			break;
		case 3:
			prepareGoodList();
			break;
		case 4:
			if (customer == null) {
				System.out.println("--------------------------------------------------------");
				System.out.println("------      登录都还没有登录,买个篮子啊      ----------------");
				System.out.println("--------------------------------------------------------");
			} else {
				ensureGoodMessage();
			}
			break;
		case 5:
			if (customer == null) {
				System.out.println("--------------------------------------------------------");
				System.out.println("------      看什么看! 想看就登陆去              ----------------");
				System.out.println("--------------------------------------------------------");
			} else {
				prepareCartlist();
			}
			break;
		case 6:
			if (customer == null) {
				System.out.println("--------------------------------------------------------");
				System.out.println("------      看什么看! 想看就登陆去              ----------------");
				System.out.println("--------------------------------------------------------");
			} else {
				perpareCreditsList();
			}
			break;
		case 7:
			break;
		case 8:
			break;
		case 9:
		default:
			customer = null;
			System.out.println("--------------------------------------------------------");
			System.out.println("-----                再见   , 朋友                ----------------");
			System.out.println("--------------------------------------------------------");
			break;
		}
		return choose;
	}

	private void userEnterFace() {
		System.out.println("--------------------------------------------------------");
		System.out.println("------              请选择你的操作                                   -----");
		System.out.println("--------------------------------------------------------");
		System.out.println("----1) 注册                                          2) 登录                         -----");
		System.out.println("----3) 查询商品                                   4) 购买商品                 -----");
		System.out.println("----5) 查看购物车                               6) 积分记录查询          -----");
		System.out.println("----7) 个人信息修改                           8) 密码修改                  -----");
		System.out.println("----9) 返回上一级                                                                      -----");
		System.out.println("--------------------------------------------------------");
		System.out.println("----[ 输入了其他会直接返回上一级哦~ ]                -----");
		System.out.println("--------------------------------------------------------");
	}

	private void regFace() {
		System.out.println("--------------------------------------------------------");
		System.out.println("----   啊咧!! 有人来注册啦! -----------------------------");
		System.out.println("--------------------------------------------------------");
	}

	private void regOpeartor() {
		in = new Scanner(System.in);
		String account = null;
		String password1 = null, password2;
		ICustomerService customerService = new ICustomerServiceImpl();
		for (int i = 0; i < 3; i++) {
			System.out.println("--------------------------------------------------------");
			System.out.print("请输入你想要注册的账号:");
			account = in.nextLine();
			if (customerService.findByUserAcount(account) == null) {
				System.out.println("--------------------------------------------------------");
				System.out.println("--        这个用户名可以注册~  请继续~      -------------");
				System.out.println("--------------------------------------------------------");
				break;
			} else if (i < 2) {
				System.out.println("--------------------------------------------------------");
				System.out.println("--不好意思啦~ 这个用户名被其他用户注册啦~ 再想一个,怎么样--");
				System.out.println("--------------------------------------------------------");
			} else {
				System.out.println("--------------------------------------------------------");
				System.out.println("--  不好意思啦~ 你再想想再来呗~                        ---");
				System.out.println("--------------------------------------------------------");
			}
		}
		for (int i = 0; i < 3; i++) {
			System.out.println("--------------------------------------------------------");
			System.out.print("请输入密码:");
			password1 = in.nextLine();
			System.out.print("请再次输入密码:");
			password2 = in.nextLine();

			if (password1.equals(password2)) {
				break;
			} else if (i < 2) {
				System.out.println("--------------------------------------------------------");
				System.out.println("--  你两次输入的密码不一样咧~ 认真看看?                ---");
				System.out.println("--------------------------------------------------------");
			} else {
				System.out.println("--------------------------------------------------------");
				System.out.println("--  三次都不对啦!   去想清楚再来吧...                  ---");
				System.out.println("--------------------------------------------------------");
			}
		}

		Customer customer = new Customer(account, password1, null, null, new Date());
		try {
			customerService.add(customer);
			System.out.println("--------------------------------------------------------");
			System.out.println("--          注册成功啦!   欢迎你的加入!                ---");
			System.out.println("--------------------------------------------------------");
		} catch (SQLException e) {
			System.out.println("--------------------------------------------------------");
			System.out.println("--          注册不小心失败啦...  你再注册一次好吗..    ---");
			System.out.println("--------------------------------------------------------");
		}
	}

	private void perpareCreditsList() {
		List<Credits> list;
		ICreditsService creditsService = new ICreditsServiceImpl();
		list = creditsService.findByUserID(customer.getUserId());
		showCreditsList(list);
	}

	private void showCreditsList(List<Credits> list) {
		int curPage = 1;
		list = list != null ? list : new ArrayList<>();
		int pageNum = (list.size() % 10) != 0 ? (list.size() / 10 + 1) : (list.size() / 10);
		in = new Scanner(System.in);
		while (curPage != 0) {
			System.out.println("--------------------------------------------------------");
			System.out.println("------               你的积分记录列表共 " + pageNum + " 页                 ------");
			System.out.println("--------------------------------------------------------");

			System.out.println("积分编号\t|\t积分类型\t|\t积分值\t|\t积分获取时间");

			for (int i = (curPage - 1) * 10; i < (curPage - 1) * 10 + 10 && i < list.size(); i++) {
				Credits credit = list.get(i);
				System.out.println(credit.getId() + "\t|\t" + credit.getType() + "\t|\t" + credit.getValue() + "\t|\t"
						+ format.format(credit.getTime()));
			}
			System.out.println("--------------------------------------------------------");
			System.out.println("------                 第 " + pageNum + " 页                 ------------------");
			System.out.println("------     你可以选择查看页数,或者输入0退出                   -----");
			System.out.println("--------------------------------------------------------");
			System.out.print("请输入:");
			try {
				curPage = in.nextInt();
				if (curPage > pageNum) {
					System.out.println("页码过大! 退出");
					break;
				}
			} catch (InputMismatchException e) {
				curPage = 0;
				System.out.println("这么调皮的....返回上一级!");
			}
		}
	}

	private void prepareCartlist() {
		ICartService cartService = new ICartServiceImpl();
		List<Cart> list = cartService.findByUserId(customer.getUserId());
		showCartLsit(list);
	}

	private void showCartLsit(List<Cart> list) {
		int curPage = 1;
		list = list != null ? list : new ArrayList<>();
		int pageNum = (list.size() % 10) != 0 ? (list.size() / 10 + 1) : (list.size() / 10);
		while (curPage != 0) {
			System.out.println("--------------------------------------------------------");
			System.out.println("-------               你的购物车列表 " + pageNum + " 页               --------");
			System.out.println("--------------------------------------------------------");

			System.out.println("id\t|\t商品名\t\t|\t购买时间\t\t|\t购买数量");

			for (int i = (curPage - 1) * 10; i < (curPage - 1) * 10 + 10 && i < list.size(); i++) {
				Cart cart = list.get(i);
				System.out.println(cart.getId() + "\t|\t" + cart.getGood().getName() + "\t|\t"
						+ format.format(cart.getBuyTime()) + "\t|\t" + cart.getBuyCount());
			}
			System.out.println("--------------------------------------------------------");
			System.out.println("------                 第 " + pageNum + " 页                 ------------------");
			System.out.println("------     你可以选择查看页数,或者输入0退出                   -----");
			System.out.println("--------------------------------------------------------");
			System.out.print("请输入:");
			try {
				curPage = in.nextInt();
				if (curPage > pageNum) {
					System.out.println("页码过大! 退出");
					break;
				}
			} catch (InputMismatchException e) {
				curPage = 0;
				System.out.println("这么调皮的....返回上一级!");
			}
		}
	}

	private void prepareGoodList() {
		IGoodsService goodsService = new IGoodsServiceImpl();
		List<Goods> list = goodsService.findAll();
		showGoodsList(list);
	}

	private void showGoodsList(List<Goods> list) {
		int curPage = 1;
		int pageNum = (list.size() % 10) != 0 ? (list.size() / 10 + 1) : (list.size() / 10);
		list = list != null ? list : new ArrayList<>();
		while (curPage != 0) {
			System.out.println("--------------------------------------------------------");
			System.out.println("-----------               商品列表共 " + pageNum + " 页               --------");
			System.out.println("--------------------------------------------------------");

			System.out.println("id\t|\t商品名\t\t|\t商品价格\t|\t商品类型");

			for (int i = (curPage - 1) * 10; i < (curPage - 1) * 10 + 10 && i < list.size(); i++) {
				Goods good = list.get(i);
				System.out.println(good.getGid() + "\t|\t" + good.getName() + "\t|\t" + good.getPrice() + "\t|\t"
						+ good.getType());
			}
			System.out.println("--------------------------------------------------------");
			System.out.println("------                 第 " + pageNum + " 页                 ------------------");
			System.out.println("------     你可以选择查看页数,或者输入0退出                   -----");
			System.out.println("--------------------------------------------------------");
			System.out.print("请输入:");
			try {
				curPage = in.nextInt();
				if (curPage > pageNum) {
					System.out.println("页码过大! 退出");
					break;
				}
			} catch (InputMismatchException e) {
				curPage = 0;
				System.out.println("这么调皮的....返回上一级!");
			}
		}
	}

	private void ensureGoodMessage() {
		in = new Scanner(System.in);
		int count = 0;
		Goods good = null;
		System.out.println("--------------------------------------------------------");
		good = isExist();
		if (good != null) {
			System.out.println("--------------------------------------------------------");
			System.out.print("---------   请输入购买数量 : ");
			try {
				count = in.nextInt();
				if (count <= 0) {
					System.out.println("信息输入有误,返回上一级!");
				}
				System.out.println("--------------------------------------------------------");
				ensureCartMessage(good, count);
			} catch (InputMismatchException e) {
				System.out.println("信息输入有误,返回上一级!");
			}
		}
	}

	private void ensureCartMessage(Goods good, int count) {
		System.out.println("--------------------------------------------------------");
		System.out.println("------------         请核对你的购买信息                  ---------");
		System.out.println("--------------------------------------------------------");

		in = new Scanner(System.in);
		int choose;

		System.out.println("商品名\t\t|\t商品数量\t|\t总价");
		System.out.println(good.getName() + "\t|\t" + count + "\t|\t" + (good.getPrice() * count));
		System.out.println("--------------------------------------------------------");
		System.out.println("----     确认购买请输入:1    输入其它则为取消购买    ---------");
		System.out.println("--------------------------------------------------------");

		try {
			choose = in.nextInt();
		} catch (InputMismatchException e) {
			choose = 0;
		}

		if (choose == 1) {
			ICartService cartService = new ICartServiceImpl();
			Cart cart = new Cart(customer.getUserId(), good.getGid(), new Date(), count);
			try {
				cartService.add(cart);
			} catch (SQLException e) {
				System.out.println("不好意思,购买失败!请重新再试,谢谢~");
			}
		} else {
			System.out.println("订单已取消!");
		}
	}

	private Goods isExist() {
		IGoodsService goodsService = new IGoodsServiceImpl();
		in = new Scanner(System.in);
		int goodId;
		Goods good = null;

		for (int i = 0; i < 3; i++) {
			System.out.print("------    请输入你想购买的商品id : ");
			try {
				goodId = in.nextInt();
			} catch (InputMismatchException e) {
				return null;
			}
			good = goodsService.findByGoodId(goodId);
			if (good != null) {
				return good;
			} else if (i < 2) {
				System.out.println("--------------------------------------------------------");
				System.out.println("--          你输入的商品不存在咧~ 认真看看?        -------");
				System.out.println("--------------------------------------------------------");
			} else {
				System.out.println("--------------------------------------------------------");
				System.out.println("-----------    三次输入错误,请返回重试          --------------");
				System.out.println("--------------------------------------------------------");
				return null;
			}
		}
		return null;
	}

	private void adminEnterFace() {
		System.out.println("--------------------------------------------------------");
		System.out.println("------              请选择你的操作                                   -----");
		System.out.println("--------------------------------------------------------");
		System.out.println("----1) 商品管理                                   2) 用户管理                 -----");
		System.out.println("----3) 查询订单                                   4) 管理员注册              -----");
		System.out.println("----5) 管理员管理                               6) 个人信息管理          -----");
		System.out.println("----7) 修改密码                                   8) 返回上一级              -----");
		System.out.println("--------------------------------------------------------");
		System.out.println("----[ 输入了其他会直接返回上一级哦~ ]                -----");
		System.out.println("--------------------------------------------------------");
	}

	private void adminEnter() {
		int choose;
		in = new Scanner(System.in);
		try {
			System.out.print("请输入你的选择:");
			choose = in.nextInt();
		} catch (InputMismatchException e) {
			choose = 5;
			System.out.println("输入错误啦~ 不开玩笑!");
		}

		switch (choose) {
		case 1:

			break;
		case 2:

			break;
		case 3:

			break;
		case 4:
			regiserAdmin();
			break;
		case 5:
			break;
		case 6:
			break;
		case 7:
			break;
		case 8:
		default:
			admin = null;
			break;
		}
	}

	private void regiserAdmin() {
		in = new Scanner(System.in);
		String account = null;
		String password1 = null, password2;
		IAdminService adminService = new IAdminServiceImpl();
		for (int i = 0; i < 3; i++) {
			System.out.println("--------------------------------------------------------");
			System.out.print("请输入你想要注册的账号:");
			account = in.nextLine();
			if (adminService.findByAdminAccount(account) == null) {
				System.out.println("--------------------------------------------------------");
				System.out.println("--        这个用户名可以注册~  请继续~      -------------");
				System.out.println("--------------------------------------------------------");
				break;
			} else if (i < 2) {
				System.out.println("--------------------------------------------------------");
				System.out.println("--不好意思啦~ 这个用户名被其他用户注册啦~ 再想一个,怎么样--");
				System.out.println("--------------------------------------------------------");
			} else {
				System.out.println("--------------------------------------------------------");
				System.out.println("--  不好意思啦~ 你再想想再来呗~                        ---");
				System.out.println("--------------------------------------------------------");
			}
		}
		for (int i = 0; i < 3; i++) {
			System.out.println("--------------------------------------------------------");
			System.out.print("请输入密码:");
			password1 = in.nextLine();
			System.out.print("请再次输入密码:");
			password2 = in.nextLine();

			if (password1.equals(password2)) {
				break;
			} else if (i < 2) {
				System.out.println("--------------------------------------------------------");
				System.out.println("--  你两次输入的密码不一样咧~ 认真看看?                ---");
				System.out.println("--------------------------------------------------------");
			} else {
				System.out.println("--------------------------------------------------------");
				System.out.println("--  三次都不对啦!   去想清楚再来吧...                  ---");
				System.out.println("--------------------------------------------------------");
			}
		}

		Admin admin = new Admin(account, password1, null, null, new Date());
		try {
			adminService.add(admin);
			System.out.println("--------------------------------------------------------");
			System.out.println("--          注册成功啦!   欢迎你  新管理员!             ---");
			System.out.println("--------------------------------------------------------");
		} catch (SQLException e) {
			System.out.println("--------------------------------------------------------");
			System.out.println("--          注册不小心失败啦...  你再注册一次好吗..    ---");
			System.out.println("--------------------------------------------------------");
		}
	}

}
